<?php


namespace addons\Minerva\api\modules\v1\controllers;

use addons\Minerva\common\models\MinervaFtpServers;
use common\helpers\ResultHelper;
use Yii;
use api\controllers\OnAuthController;
use yii\data\ActiveDataProvider;

/**
 * ftp服务器
 *
 * Class FtpController
 * @Package addons\Minerva\api\modules\v1\controllers
 * @Author 小武哥 <1240035958@qq.com>
 */
class FtpController extends OnAuthController
{
    /**
     * @var MinervaFtpServers
     */
    public $modelClass = MinervaFtpServers::class;

    public function actionIndex()
    {
        $list = new ActiveDataProvider([
            'query' => MinervaFtpServers::find()
//                ->where(['userid' => $member_id])
                ->orderBy('created_at desc')
//                ->select('id,jiben_name,jiben_idcard,created_at,state')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);

//        $model = $list->getModels();
        return $list;
    }

    public function actionDetail(){
        $id = Yii::$app->request->get('id');
        $model = new MinervaFtpServers();
        $model = $model::findOne(['id' => $id]);
        return $model;
    }

    /**
     * 编辑/创建
     *
     * @return mixed
     */
    public function actionEdit()
    {
        $post = Yii::$app->request->post();
        $model = new MinervaFtpServers();
        if($post['id']){
            $model = $model::find()
                ->where(['id' => $post['id']])
                ->one();
        }
        $model->attributes = $post;
        if ($model->save()) {
            return $model;
        }else{
            return ResultHelper::json(422, $this->getError($model));
        }
    }
}